package com.pangus.toolkits.files.utils;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import okhttp3.Headers;

import javax.annotation.Nullable;

import static com.pangus.toolkits.files.utils.HttpHeaders.CONTENT_DISPOSITION;

/**
 * @author yingheng chen
 * @description
 * @date 2019/12/3
 */
public class ResponseUtil {
    public static @Nullable
    String getFileNameFromDisposition(Headers headers) {
        String fileName = null;
        final String desposition = headers.get(CONTENT_DISPOSITION);
        if (StrUtil.isNotBlank(desposition)) {
            fileName = ReUtil.get("filename=\"(.*?)\"", desposition, 1);
            if (StrUtil.isBlank(fileName)) {
                fileName = StrUtil.subAfter(desposition, "filename=", true);
            }
        }
        return fileName;
    }


}
